﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Library;
using System.Data;
using IHISCETeim.BLL;


namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class FTradeCatheringListEasyUI : EasyUIPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看, Permission.修改, Permission.新增, Permission.删除 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "FTradeCatheringListEasyUI";
            }
        }

        #region 属性 (查询条件)


        protected string JingXiaoShang
        {
            get
            {
                return GetRequestValue<string>("JingXiaoShang", "");

            }
        }
        protected string KuanXiangLeiXing
        {
            get
            {
                return GetRequestValue<string>("KuanXiangLeiXing", "");

            }
        }

        protected string State
        {
            get
            {
                return GetRequestValue<string>("State", "");
            }
        }


        protected string ShoukuanRiQiFrom
        {
            get
            {
                return GetRequestValue<string>("ShoukuanRiQiFrom", "");

            }
        }
        protected string ShoukuanRiQiTo
        {
            get
            {
                if (string.IsNullOrEmpty(ShoukuanRiQiFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("ShoukuanRiQiTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(ShoukuanRiQiFrom).AddDays(1).ToShortDateString();
                    else
                        return todate;
                }

            }
        }
        /// <summary>
        /// 记帐日期 表FTradeBalance
        /// </summary>
        protected string CreateDateFrom
        {
            get
            {
                return GetRequestValue<string>("CreateDateFrom", "");

            }
        }
        /// <summary>
        /// 记帐日期 表FTradeBalance
        /// </summary>
        protected string CreateDateTo
        {
            get
            {
                if (string.IsNullOrEmpty(CreateDateFrom))
                {
                    return "";
                }
                else
                {
                    string todate = GetRequestValue<string>("CreateDateTo", "");
                    if (string.IsNullOrEmpty(todate))
                        return Convert.ToDateTime(CreateDateFrom).AddDays(1).ToShortDateString();
                    else
                        return todate;
                }

            }
        }
        /// <summary>
        /// 原收款ID,如果大于0,则表示用于查看调帐关联记录
        /// </summary>
        protected int FCatheringId
        {
            get
            {
                return GetRequestValue<int>("FCatheringId", 0);
            }
        }

        #endregion

        /// <summary>
        /// 排序字段 值为 id
        /// </summary>
        protected override string orderField
        {
            get
            {
                return string.IsNullOrEmpty(base.orderField) ? "FCatheringId" : base.orderField;
            }
        }
        /// <summary>
        ///  排序方式 desc或 asc
        /// </summary>
        protected override string orderDirection
        {
            get
            {
                return string.IsNullOrEmpty(base.orderDirection) ? "DESC" : base.orderDirection;
            }
        }
        
        
        protected override string SQLSort
        {
            get
            {
                return (orderField + " " + orderDirection) ;
            }
        }

        protected string GetSQLWhere()
        {
            //营业经理权限 2014-02-21
            string agentList = base.FTradeAgentPowerSqlWhere();
            string wheresql = string.IsNullOrEmpty(agentList) ? " 1=1 " : string.Format(" JingXiaoShang in ({0})", agentList);

            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(JingXiaoShang))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("JingXiaoShang", QueryOperatorsEnum.等于, JingXiaoShang);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }



            if (!string.IsNullOrEmpty(KuanXiangLeiXing))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator(" KuanXiangLeiXing ", QueryOperatorsEnum.等于, KuanXiangLeiXing);
                if (KuanXiangLeiXing == "其他")
                {
                    DOWNSQL = " KuanXiangLeiXing not in('电汇','汇票','融资','调帐') ";
                }
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }


            if (!string.IsNullOrEmpty(ShoukuanRiQiFrom))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator(" ShoukuanRiQi ", QueryOperatorsEnum.范围, ShoukuanRiQiFrom + "|" + ShoukuanRiQiTo);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }

            if (!string.IsNullOrEmpty(State))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator(" State ", QueryOperatorsEnum.等于, State);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }

            return wheresql;
        }

        protected string GetSqlWhere2()
        {
            if (!string.IsNullOrEmpty(CreateDateFrom))
            {
                string DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator(" CreateDate ", QueryOperatorsEnum.范围, CreateDateFrom + "|" + CreateDateTo);

                return DOWNSQL;
            }
            return "";
        }
        protected override string query()
        {
            DataTableCollection dts = null;

            if (FCatheringId > 0)
            {
                dts = FTradeCatheringController.GetHistoryPagination(" OldFCatheringId=" + FCatheringId.ToString(), pageNum, numPerPage, SQLSort, ref TotalCount);
                return ToDataGridJson(dts[0], TotalCount, null).ToString();
            }
            else
            {
                dts = FTradeCatheringController.GetPagination(GetSQLWhere(),GetSqlWhere2(), pageNum, numPerPage, SQLSort, ref TotalCount);
                return ToDataGridJson(dts[0], TotalCount, dts[1]).ToString();
            }
           // return ToDataGridJson(dts[0], TotalCount, dts[1]).ToString();
        }
    }
}